*********


*Set root data directory
local rootdir 
cd "`rootdir'districts"




set mem 10g


****Part 1: reading in raw ICRISTA VDSA district data, by state******


******Andhra Pradesh
cd "`rootdir'districts\Andhra Pradesh"



insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
save "`rootdir'districts\alldata", replace



*************Assam

cd "`rootdir'districts\Assam"


insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*note: error in VDSA area-prod-a file -- need to drop last columns

# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
cap: destring ppea_tp, replace force;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr

insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata

drop v1* v2* v3* v8 v9

compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


****Bihar


*area-prod has garbage in last columns

cd "`rootdir'districts\Bihar"
insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr

insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace



*********Chhatisgarh
cd "`rootdir'districts\Chhattisgarh"

insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr

insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12 v13 v60 v61 v62
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


*****Gujarat

cd "`rootdir'districts\Gujarat"

insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr




insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12
destring june, force replace
destring fmlt_tp, force replace

compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace




cd "`rootdir'districts\Haryana"

insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


****Himachal Pradesh

cd "`rootdir'districts\Himachal Pradesh"


insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v2* v3*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace



****Jhakhand
cd "`rootdir'districts\Jharhkand"



insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fhp-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
cap: drop v12 v13;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace



***Karnataka

cd "`rootdir'districts\Karnataka"


insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr




insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata


drop v1* v6* v7* v8*

compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.

destring brly_tp, replace force

append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace






***kerala

cd "`rootdir'districts\Kerala"



insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v8* v9* v2* v3* v4* v5* v6* v7* v8*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


****Madhya Pradesh

cd "`rootdir'districts\Madhya Pradesh"


insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v2* v6* v7* 
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


****Maharashtra

cd "`rootdir'districts\Maharashtra"


insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12 v6* v7*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace



****Orissa

cd "`rootdir'districts\Orissa"


insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v6* v7*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


******Punjab


cd "`rootdir'districts\Punjab"


insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v6* v7*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


***Rajasthan

cd "`rootdir'districts\Rajasthan"




insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12 v2* v3* v6* v7*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace

***Tamil Nadu


cd "`rootdir'districts\Tamil Nadu"




insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v12 v6* v7*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace



***Utarrakhand

cd "`rootdir'districts\Utarrakhand"



insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fodder-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v6* v7* v8*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 jan
ren v8 feb
ren v9 march
ren v10 april
ren v11 may
ren v12 june
ren v13 july
ren v14 aug
ren v15 sept
ren v16 oct
ren v17 nov
ren v18 dec
ren v19 annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.



append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace



***Uttar Pradesh

cd "`rootdir'districts\Uttar Pradesh"




insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v2* v6* v7* v8*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace


****West Bengal
cd "`rootdir'districts\West Bengal"




insheet using dt-agri-implements-a.csv, delimiter(,) clear
compress 
save alldata, replace emptyok

*first do annual variables

*dt-agri-implements-a
# delimit ;
foreach j in 
dt-area-prod-a
dt-cia-a
dt-fert-consumption-a
dt-fert-prices-a
dt-fhp-a
dt-fodder-a
dt-hyv-a
dt-june-julyaug-rainfall-a
dt-landuse-a
dt-livestock-a
dt-market-road-a
dt-nca-gca-nia-gia-a
dt-operational-holdings-a
dt-population-a
dt-sia-a
dt-wages-a
{;
clear;
insheet using `j'.csv, delimiter(,);
*summ;
sleep 1000;
compress;
summ;
merge m:m stcode dist year using alldata;
drop _merge;
sleep 1000;
save alldata, replace;
};


# delimit cr



insheet using dt-aesr-a.csv, delimiter(,) clear
compress 
sleep 1000
merge 1:m stcode dist using alldata
drop v1* v6* v7* v8*
compress
sleep 1000
drop _merge
save alldata, replace


*no years

**errors because one of the variables is called long, stata will not accept this as a variable
*dt-mai-a    
clear
insheet using dt-mai-a.csv, delimiter(,) noname clear
drop if _n==1

foreach j in 1 3 5 6 7 8 9 10{
destring v`j', force replace
}



ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 mai_annrf
ren v8 mai_anndp
ren v9 mai_annual_pe
ren v10 mai_annmai
compress 
sleep 1000
merge 1:m stcode dist using alldata
compress
sleep 1000
drop _merge
save alldata, replace

*dt-normal-rainfall-a
clear
insheet using dt-normal-rainfall-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}


ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 norm_jan
ren v8 norm_feb
ren v9 norm_march
ren v10 norm_april
ren v11 norm_may
ren v12 norm_june
ren v13 norm_july
ren v14 norm_aug
ren v15 norm_sept
ren v16 norm_oct
ren v17 norm_nov
ren v18 norm_dec
ren v19 norm_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
save alldata, replace

*dt-pet-a
clear
insheet using dt-pet-a.csv, delimiter(,) noname clear
drop if _n==1
foreach j in 1 3 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19{
destring v`j', force replace
}

ren v1 stcode
ren v2 stname
ren v3 dist
ren v4 distname
ren v5 lat
ren v6 lon
ren v7 normpe_jan
ren v8 normpe_feb
ren v9 normpe_march
ren v10 normpe_april
ren v11 normpe_may
ren v12 normpe_june
ren v13 normpe_july
ren v14 normpe_aug
ren v15 normpe_sept
ren v16 normpe_oct
ren v17 normpe_nov
ren v18 normpe_dec
ren v19 normpe_annual
compress 
sleep 1000
merge 1:m stcode dist using alldata
sleep 1000
drop _merge
drop if dist==.
append using "`rootdir'districts\alldata"
save "`rootdir'districts\alldata", replace































******Part 2, data cleaning and concording*********

cd "`rootdir'districts\"
use alldata, clear

foreach i of varlist lat-tractor{
replace `i'=. if `i'==-1
}

*use the old states
*Chittoor/Cuddapah
*looks like (all) census data got switched, as did labelling
*fix this mix up (note - it could go the other way! this assumes names are correct not dist codes)
*for now assume all the data was mixed up (maybe just one part? ugh)
replace dist=54 if distname=="Chittoor"
replace dist=53 if distname=="Cuddapah"
replace distname="Cuddapah" if dist==54
replace distname="Chittoor" if dist==53
*Rajgarh - 41 in district file what's the problem? missing from the nm file! (district 5 in name file)
*Buldhana - 113 in district file (district 111 in name file)
*uttarakhand became statecode 12 in name file



*Uttarkhand-Uttar Pradesh
replace stcode=12 if stcode==16

*Jharkand-Bihar
replace stcode=2 if stcode==15

*Chhatisgarh-Madhya Pradesh
replace stcode=6 if stcode==14
replace stname="Madhya Pradesh" if stcode==6

sort distname
*browse distname

drop stname distname
merge m:1 stcode dist using district_conversion
drop _merge


sort statename districtname year


*linear interpolation for census variables
foreach j of varlist pop_t-agrl_uf{
by statename districtname: ipolate `j' year, gen(`j'_i)
replace `j'=`j'_i
drop `j'_i
}

**now drop the census odd years
drop if year==1961 | year==2011

*fixing errors for roads [error = more than 10% decline in road length]
gen flag=0
forvalues j=1(1)20{
gen dlroad`j'=(lroad-lroad[_n-`j'])/lroad[_n-`j'] if statename==statename[_n-`j'] & districtname==districtname[_n-`j'] & year==year[_n-`j']+`j'
*replace flag=1 if (dlroad`j'>0.2 & dlroad`j'<.) | dlroad`j'<-0.05
replace flag=1 if dlroad`j'<-0.1
}
gen lroadf=lroad
replace lroadf=. if flag==1
drop flag
drop dlroad*


*fit parametric trend
***1. quintic time-trend fit for roads [maybe this is too much-- puts too much weight at the end?
by statename districtname: gen period=_n
gen period2=period^2
gen period3=period^3
gen period4=period^4
gen period5=period^5
egen dist2=group(statename districtname)
egen distmax=max(dist2)
local maxdist=distmax[1]

**let's cap these at 20% to allow some room for post-growth on trend
bysort dist2: egen minlroad=min(lroad*0.8)
bysort dist2: egen maxlroad=max(lroad*1.2)
bysort dist2: egen minlroadf=min(lroadf*0.8)
bysort dist2: egen maxlroadf=max(lroadf*1.2)


gen lroad2=.
gen lroad2f=.

local pdegree 3

forvalues j=1(1)`maxdist'{

cap: reg lroad period-period`pdegree' if dist2==`j' & year<2010
cap: predict temp, xb
cap: replace lroad2=temp if dist2==`j'
cap: replace lroad2=minlroad if lroad2<minlroad
cap: replace lroad2=maxlroad if lroad2>maxlroad
cap: drop temp

cap: reg lroadf period-period`pdegree' if dist2==`j' & year<2010
cap: predict temp, xb
cap: replace lroad2f=temp if dist2==`j'
cap: replace lroad2f=minlroadf if lroad2f<minlroadf
cap: replace lroad2f=maxlroadf if lroad2f>maxlroadf
cap: drop temp
}
drop period period2 period3 dist2 period4 period5


sort statename districtname year

*fit linear interpolation, for now keep only interpolated values
foreach j of varlist male_wage-othsour marg_no-total_ar ppmrkt-ptmrkt lroad catl_t-poultry totgeog-cufalow dahcom_1-tractor daicom_3-lroadf{
by statename districtname: ipolate `j' year, gen(i`j')
replace `j'=i`j'
drop i`j'
}

ren statename statenamedist
ren districtname districtnamedist

drop minlroad* maxlroad*
summ *road*
corr *road*


save "`rootdir'districts\alldata_nm61_inter", replace





***Merge/convert districts to NSS districts*********





***Manually read districts/regions from NSS codebooks into STATA files
*nss43, nss55, nss61, nss66
cd "`rootdir'"

*66 - 618 districts
*61 - 600 districts
*55 - 506 districts
*43 - 506 districts




use "`rootdir'districts\nss66", clear
replace districtname=ltrim(districtname)
replace districtname=rtrim(districtname)
replace districtname=itrim(districtname)
**round 66 corrections for 61 compatibility
replace statename="Uttaranchal" if statename=="Uttarkhand"
replace regionname="Uttaranchal" if regionname=="Uttarkhand"

replace districtname="Champaran(E)" if districtname=="Champaran (E)"
replace districtname="Champaran(W)" if districtname=="Champaran (W)"
replace districtname="Lakhisarai" if districtname=="Lakshadweephisarai"
replace districtname="Kaimur (Bhabua)" if districtname=="Kaimur Bhabua"
replace districtname="Dadra & Nagar Haveli" if districtname=="Dadra and Nagar Havelira & NH"
replace districtname="Singhbhum(W)" if districtname=="Singhbhum (W)"
replace districtname="Bangalore (Rural)" if districtname=="Bangalore Rural"
replace districtname="Palakkad" if districtname=="PaLakshadweepkad"
replace districtname="E. Nimar (Khandwa)" if districtname=="E. Nimar"
replace districtname="W. Nimar (Khargoan)" if districtname=="W. Nimar"
replace districtname="Kandhamal (Phoolbani)" if districtname=="Kandhamal Phoolbani"
replace districtname="East (Gangtok)" if districtname=="East Gangtok"
replace districtname="North (Mongam)" if districtname=="North Mongam"
replace districtname="South (Nimachai)" if districtname=="South Nimachai"
replace districtname="West (Gyalshing)" if districtname=="West Gyalshing"
replace districtname="S.R.Nagar(Bhadohi)" if districtname=="S.R.Nagar Bhadoh"
replace districtname="Udham Singh" if districtname=="Udham Singh Nagar"
replace districtname="Hailakandi" if districtname=="HaiLakshadweepandi"

replace districtname="Chennai" if districtname=="Madras"
replace districtname="Mumbai" if districtname=="Greater Bombay"
replace districtname="Mumbai Suburban" if districtname=="Suburban Mumbai"
replace districtname="Raigad" if districtname=="Raigarh" & statename=="Maharashtra"
replace districtname="Rajgarh" if districtname=="Raigarh" & statename=="Madhya Pradesh"
*replace districtname="" if districtname==""

replace districtname="Delhi" if districtname=="Central"
replace districtname="Delhi" if districtname=="New Delhi"


save nss66_nm61, replace


use "`rootdir'districts\nss55", clear

replace districtname=ltrim(districtname)
replace districtname=rtrim(districtname)
replace districtname=itrim(districtname)

replace regionname="Chhatisgarh" if regionname=="Chattisgarh"

*round 55 corrections for round 61 compatibility
replace districtname="Anantapur" if districtname=="Anantpur"
replace districtname="Rangareddi" if districtname=="Rangareddy"
replace districtname="Visakhapatnam" if districtname=="Vishakhapatnam"
replace districtname="Changlang" if districtname=="Changlong"
replace districtname="Dibang Valley" if districtname=="Dibang"
replace districtname="Karbi Anglong" if districtname=="Karbiaglong"
replace districtname="Karimganj" if districtname=="Karimgang"
replace districtname="Marigaon" if districtname=="Morigaon"
replace districtname="North Cachar Hills" if districtname=="North Cachar"
replace districtname="Champaran(E)" if districtname=="East Champaran"
replace districtname="Champaran(W)" if districtname=="West Champaran"
replace districtname="Kaimur (Bhabua)" if districtname=="Bhabua"
replace districtname="Purnia" if districtname=="Purnea"
replace districtname="North Goa" if districtname=="Goa North"
replace districtname="South Goa" if districtname=="Goa South"
replace districtname="Bhiwani" if districtname=="Bhilwani"
replace districtname="Yamunanagar" if districtname=="Yamuna Nagar"
replace districtname="Sirmaur" if districtname=="Sirampur"
replace districtname="Baramula" if districtname=="Barmula"
replace districtname="Kathua" if districtname=="Kathus"
replace districtname="Kupwara" if districtname=="Kupwar"
replace districtname="Leh* (Ladakh)" if districtname=="Ladakh*"
replace districtname="Punch" if districtname=="Poonch"
replace districtname="Rajauri" if districtname=="Rajouri"
replace districtname="Dakshina Kannada" if districtname=="Dakshin Kannad"
replace districtname="Mandya" if districtname=="Mandhya"
replace districtname="Uttara Kannada" if districtname=="Uttar Kannad"
replace districtname="Alappuzha" if districtname=="Alappuzm"
replace districtname="Kasaragod" if districtname=="Kasargod"
replace districtname="Malappuram" if districtname=="Malapuram"
replace districtname="Thrissur" if districtname=="Trichur"
replace districtname="Thiruvananthapuram" if districtname=="Triruvanantha-"
replace districtname="E. Nimar (Khandwa)" if districtname=="Khandwa (E. Nimar)"
replace districtname="W. Nimar (Khargoan)" if districtname=="Khargone (W. Nimar)"
replace districtname="Shahdol" if districtname=="Shadol"
replace districtname="Nashik" if districtname=="Nasik"
replace districtname="Raigarh" if districtname=="Raigarh (Kulaba)"
replace districtname="Mokokchung" if districtname=="Mukokchung"
replace districtname="Zunheboto" if districtname=="Zunhehoto"
replace districtname="Anugul" if districtname=="Angul"
replace districtname="Balangir" if districtname=="Bolangir"
replace districtname="Baudh" if districtname=="Boudh"
replace districtname="Debagarh" if districtname=="Deogarh"
replace districtname="Gajapati" if districtname=="Gajipati"
replace districtname="Jagatsinghapur" if districtname=="Jagatsinghpura"
replace districtname="Jajapur" if districtname=="Jajpur"
replace districtname="Kendujhar" if districtname=="Keonjhar"
replace districtname="Khordha" if districtname=="Khurda"
replace districtname="Nabarangapur" if districtname=="Nowarangpur"
replace districtname="Nuapada" if districtname=="Nuapara"
replace districtname="Kandamal (Phoolbani)" if districtname=="Phulbani"
replace districtname="Ryagada" if districtname=="Rayagarh"
replace districtname="Sonapur" if districtname=="Sonepur"
replace districtname="Bathinda" if districtname=="Bhatinda"
replace districtname="Faridkot" if districtname=="Faridkot Sahib"
replace districtname="Dhaulpur" if districtname=="Dholpur"
replace districtname="Jhunjhunun" if districtname=="Jhunjhuna"
replace districtname="Chennai" if districtname=="Chengai Anna (Chengalpattu)"
replace districtname="Dharmapuri" if districtname=="Dharampuri"
replace districtname="Dindigul" if districtname=="Dindigul-Quide (Milleth Anna)"
replace districtname="Kanniyakumari" if districtname=="Kannyai-Kumari"
replace districtname="Nagapattinam" if districtname=="Nagapatti-nam-Quaie-E-Milleth"
replace districtname="Ramanathapuram" if districtname=="Ramnathapuram"
replace districtname="The Nilgiris" if districtname=="Nilgiri"
replace districtname="Tiruchirappalli" if districtname=="Tiruchirapalli"
replace districtname="Tirunelveli" if districtname=="Tirunelveli (Kottabomman)"
replace districtname="Tiruvanamalai" if districtname=="Thiruvannamalai"
replace districtname="Viluppuram" if districtname=="Villupuram"
replace districtname="Barabanki" if districtname=="Bara Banki"
replace districtname="Deoria" if districtname=="Deeria"
replace districtname="Fatehpur" if districtname=="Fatepur"
replace districtname="Jalaun" if districtname=="Jalan"
replace districtname="Rae Bareli" if districtname=="Rai Bareli"
replace districtname="Siddharthnagar" if districtname=="Sidhartha nagar"
replace districtname="Sonbhadra" if districtname=="Sonbadra"
replace districtname="Varanasi" if districtname=="Varanashi"
replace districtname="North 24-Parganas" if districtname=="24-Parganas (North)"
replace districtname="South 24-Parganas" if districtname=="24-Parganas (South)"
replace districtname="Hugli" if districtname=="Hooghly"
replace districtname="Koch Bihar" if districtname=="Kochbihar"
replace districtname="Medinipur" if districtname=="Midnapur"
replace districtname="Nadia" if districtname=="Nandia"
replace districtname="Kolkata" if districtname=="Calcutta"
replace districtname="Chitrakoot" if districtname=="Chitrakut"
replace districtname="Hazaribag" if districtname=="Hazaribagh"
replace districtname="Pakaur" if districtname=="Pakur"
replace districtname="Rajnandgaon" if districtname=="Raj Nandgaon"
replace districtname="Ranchi" if districtname=="Ranchi (Purbi)"
replace districtname="Rayagada" if districtname=="Ryagada"
replace districtname="Sahibganj" if districtname=="Sahibaganj"
replace districtname="Singhbhum (E)" if districtname=="Singhbhum"
replace districtname="Singhbhum(W)" if districtname=="Singhbhum (Paschim)"
replace districtname="Sitamarhi" if districtname=="Sitamari"
replace districtname="Uttarkashi" if districtname=="Uttar Kashi"
replace districtname="Dehradun" if districtname=="Dehra Dun"


replace districtname="Chengalpattu" if districtname=="Chengai Anna (Chengalpattu)"


replace districtname="Nagaon" if districtname=="Nowgong"

replace districtname="Chennai" if districtname=="Madras"
replace districtname="Mumbai" if districtname=="Greater Bombay"
replace districtname="Mumbai Suburban" if districtname=="Suburban Mumbai"
replace districtname="Raigad" if districtname=="Raigarh" & statename=="Maharashtra"

save nss55_nm61, replace




use "`rootdir'districts\nss43", clear

replace districtname=ltrim(districtname)
replace districtname=rtrim(districtname)
replace districtname=itrim(districtname)


replace statename="Goa" if districtname=="Goa"
replace statename="Daman & Diu" if districtname=="Daman" | districtname=="Diu"

replace regionname="Chhatisgarh" if regionname=="Chattisgarh"



*round 55 corrections for round 61 compatibility
replace districtname="Anantapur" if districtname=="Anantpur"
replace districtname="Rangareddi" if districtname=="Rangareddy"
replace districtname="Visakhapatnam" if districtname=="Vishakhapatnam"
replace districtname="Changlang" if districtname=="Changlong"
replace districtname="Dibang Valley" if districtname=="Dibang"
replace districtname="Karbi Anglong" if districtname=="Karbiaglong"
replace districtname="Karimganj" if districtname=="Karimgang"
replace districtname="Marigaon" if districtname=="Morigaon"
replace districtname="North Cachar Hills" if districtname=="North Cachar"
replace districtname="Champaran(E)" if districtname=="East Champaran"
replace districtname="Champaran(W)" if districtname=="West Champaran"
replace districtname="Kaimur (Bhabua)" if districtname=="Bhabua"
replace districtname="Purnia" if districtname=="Purnea"
replace districtname="North Goa" if districtname=="Goa North"
replace districtname="South Goa" if districtname=="Goa South"
replace districtname="Bhiwani" if districtname=="Bhilwani"
replace districtname="Yamunanagar" if districtname=="Yamuna Nagar"
replace districtname="Sirmaur" if districtname=="Sirampur"
replace districtname="Baramula" if districtname=="Barmula"
replace districtname="Kathua" if districtname=="Kathus"
replace districtname="Kupwara" if districtname=="Kupwar"
replace districtname="Leh* (Ladakh)" if districtname=="Ladakh*"
replace districtname="Punch" if districtname=="Poonch"
replace districtname="Rajauri" if districtname=="Rajouri"
replace districtname="Dakshina Kannada" if districtname=="Dakshin Kannad"
replace districtname="Mandya" if districtname=="Mandhya"
replace districtname="Uttara Kannada" if districtname=="Uttar Kannad"
replace districtname="Alappuzha" if districtname=="Alappuzm"
replace districtname="Kasaragod" if districtname=="Kasargod"
replace districtname="Malappuram" if districtname=="Malapuram"
replace districtname="Thrissur" if districtname=="Trichur"
replace districtname="Thiruvananthapuram" if districtname=="Triruvanantha-"
replace districtname="E. Nimar (Khandwa)" if districtname=="Khandwa (E. Nimar)"
replace districtname="W. Nimar (Khargoan)" if districtname=="Khargone (W. Nimar)"
replace districtname="Shahdol" if districtname=="Shadol"
replace districtname="Nashik" if districtname=="Nasik"
replace districtname="Raigarh" if districtname=="Raigarh (Kulaba)"
replace districtname="Mokokchung" if districtname=="Mukokchung"
replace districtname="Zunheboto" if districtname=="Zunhehoto"
replace districtname="Anugul" if districtname=="Angul"
replace districtname="Balangir" if districtname=="Bolangir"
replace districtname="Baudh" if districtname=="Boudh"
replace districtname="Debagarh" if districtname=="Deogarh"
replace districtname="Gajapati" if districtname=="Gajipati"
replace districtname="Jagatsinghapur" if districtname=="Jagatsinghpura"
replace districtname="Jajapur" if districtname=="Jajpur"
replace districtname="Kendujhar" if districtname=="Keonjhar"
replace districtname="Khordha" if districtname=="Khurda"
replace districtname="Nabarangapur" if districtname=="Nowarangpur"
replace districtname="Nuapada" if districtname=="Nuapara"
replace districtname="Kandamal (Phoolbani)" if districtname=="Phulbani"
replace districtname="Ryagada" if districtname=="Rayagarh"
replace districtname="Sonapur" if districtname=="Sonepur"
replace districtname="Bathinda" if districtname=="Bhatinda"
replace districtname="Faridkot" if districtname=="Faridkot Sahib"
replace districtname="Dhaulpur" if districtname=="Dholpur"
replace districtname="Jhunjhunun" if districtname=="Jhunjhuna"
replace districtname="Chennai" if districtname=="Chengai Anna (Chengalpattu)"
replace districtname="Dharmapuri" if districtname=="Dharampuri"
replace districtname="Dindigul" if districtname=="Dindigul-Quide (Milleth Anna)"
replace districtname="Kanniyakumari" if districtname=="Kannyai-Kumari"
replace districtname="Nagapattinam" if districtname=="Nagapatti-nam-Quaie-E-Milleth"
replace districtname="Ramanathapuram" if districtname=="Ramnathapuram"
replace districtname="The Nilgiris" if districtname=="Nilgiri"
replace districtname="Tiruchirappalli" if districtname=="Tiruchirapalli"
replace districtname="Tirunelveli" if districtname=="Tirunelveli (Kottabomman)"
replace districtname="Tiruvanamalai" if districtname=="Thiruvannamalai"
replace districtname="Viluppuram" if districtname=="Villupuram"
replace districtname="Barabanki" if districtname=="Bara Banki"
replace districtname="Deoria" if districtname=="Deeria"
replace districtname="Fatehpur" if districtname=="Fatepur"
replace districtname="Jalaun" if districtname=="Jalan"
replace districtname="Rae Bareli" if districtname=="Rai Bareli"
replace districtname="Siddharthnagar" if districtname=="Sidhartha nagar"
replace districtname="Sonbhadra" if districtname=="Sonbadra"
replace districtname="Varanasi" if districtname=="Varanashi"
replace districtname="North 24-Parganas" if districtname=="24-Parganas (North)"
replace districtname="South 24-Parganas" if districtname=="24-Parganas (South)"
replace districtname="Hugli" if districtname=="Hooghly"
replace districtname="Koch Bihar" if districtname=="Kochbihar"
replace districtname="Medinipur" if districtname=="Midnapur"
replace districtname="Nadia" if districtname=="Nandia"
replace districtname="Kolkata" if districtname=="Calcutta"
replace districtname="Chitrakoot" if districtname=="Chitrakut"
replace districtname="Hazaribag" if districtname=="Hazaribagh"
replace districtname="Pakaur" if districtname=="Pakur"
replace districtname="Rajnandgaon" if districtname=="Raj Nandgaon"
replace districtname="Ranchi" if districtname=="Ranchi (Purbi)"
replace districtname="Rayagada" if districtname=="Ryagada"
replace districtname="Sahibganj" if districtname=="Sahibaganj"
replace districtname="Sitamarhi" if districtname=="Sitamari"
replace districtname="Uttarkashi" if districtname=="Uttar Kashi"
replace districtname="Dehradun" if districtname=="Dehra Dun"

replace districtname="Kandhamal (Phoolbani)" if districtname=="Khondmals"
replace districtname="Kandhamal (Phoolbani)" if districtname=="Boudh (Khondmals)"

replace districtname="Nagaon" if districtname=="Nowgong"

replace districtname="Chennai" if districtname=="Madras"
replace districtname="Mumbai" if districtname=="Greater Bombay"
replace districtname="Mumbai Suburban" if districtname=="Suburban Mumbai"
replace districtname="Raigad" if districtname=="Raigarh" & statename=="Maharashtra"


replace districtname="Chhimtupui" if districtname=="Chhimtuipui"


replace districtname="Darrang" if districtname=="Darrag"
replace districtname="Chengalpattu" if districtname=="Anna"
replace districtname="Pasungpon" if districtname=="Ramalingam"
replace districtname="Tirunelveli" if districtname=="Tirunelvalli"

replace districtname="Singbhum" if districtname=="Singhbhum"

save nss43_nm61, replace









use "`rootdir'districts\nss61", clear
replace districtname="Raigad" if districtname=="Raigarh" & statename=="Maharashtra"
replace districtname="Raigad" if districtname=="Rajgad" & statename=="Maharashtra"
replace districtname="Rajgarh" if districtname=="Raigarh" & statename=="Madhya Pradesh"
replace districtname="Mumbai Suburban" if districtname=="Suburban Mumbai"

*need to collapse Delhi Central/East/North/New Delhi because district codes missing (replaced with 99)
replace districtname="Delhi" if districtname=="Central" & statename=="Delhi"
replace districtname="Delhi" if districtname=="East" & statename=="Delhi"
replace districtcode=99 if districtname=="Delhi"
replace districtname="Delhi" if districtname=="New Delhi" & statename=="Delhi"
replace districtname="Delhi" if districtname=="North" & statename=="Delhi"
replace districtcode=98 if districtname=="Delhi" & districtcode~=99
save nss61_nm61, replace





************Districts***
**step 1: match by state and district name, check consistency
*matching by name 
*66 61 55 43

*done

**step 2: concordance to 1966 consolidated districts, district names in ICRISAT and (older) India climate database


use "`rootdir'districts\alldata", clear
foreach j of varlist  ppmrkt-cufalow{
replace `j'=. if `j'==-1
}

*Chittoor/Cuddapah
*looks like (all) census data got switched, as did labelling
*fix this mix up (note - it could go the other way! this assumes names are correct not dist codes)
*for now assume all the data was mixed up (maybe just one part? ugh)
replace dist=54 if distname=="Chittoor"
replace dist=53 if distname=="Cuddapah"
replace distname="Cuddapah" if dist==54
replace distname="Chittoor" if dist==53

*use the old states
*Uttarkhand-Uttar Pradesh
replace stcode=12 if stcode==16
replace stname="Uttar Pradesh" if stcode==12

*Jharkand-Bihar
replace stcode=2 if stcode==15
replace stname="Bihar" if stcode==2

*Chhatisgarh-Madhya Pradesh
replace stcode=6 if stcode==14
replace stname="Madhya Pradesh" if stcode==6

save "`rootdir'districtdata_clean", replace







****Fix names***
cd "`rootdir'"

use districtdata_clean, clear
gen statename=stname
gen districtname=distname

replace districtname="Buldana" if districtname=="Buldhana"
replace districtname="Bastar" if districtname=="Baster"
replace districtname="Mahbubnagar" if dist==58
replace districtname="Munger" if dist==908
replace districtname="Palamu" if dist==915
replace districtname="Santhal Parganas" if dist==910
replace districtname="Singbhum" if dist==917
replace districtname="E. Nimar (Khandwa)" if dist==36
replace districtname="W. Nimar (Khargoan)" if dist==35
replace districtname="Bid" if dist==110
replace districtname="Baleshwar" if dist==701
replace districtname="Firozpur" if dist==172
replace districtname="Jhunjhunun" if dist==155
replace districtname="Chengalpattu" if dist==83
replace districtname="Almora" if dist==238
replace districtname="Pithoragarh" if dist==239
replace districtname="Tehri Garhwal" if dist==242
replace districtname="Dehradun" if dist==244
replace districtname="24-Parganas" if dist==800
replace districtname="Koch Bihar" if dist==816
replace districtname="Pauri Gahrwal" if dist==243


replace districtname="Anantapur" if districtname=="Anantpur"
replace districtname="Rangareddi" if districtname=="Rangareddy"
replace districtname="Visakhapatnam" if districtname=="Vishakhapatnam"
replace districtname="Changlang" if districtname=="Changlong"
replace districtname="Dibang Valley" if districtname=="Dibang"
replace districtname="Karbi Anglong" if districtname=="Karbiaglong"
replace districtname="Karimganj" if districtname=="Karimgang"
replace districtname="Marigaon" if districtname=="Morigaon"
replace districtname="North Cachar Hills" if districtname=="North Cachar"
replace districtname="Champaran(E)" if districtname=="East Champaran"
replace districtname="Champaran(W)" if districtname=="West Champaran"
replace districtname="Kaimur (Bhabua)" if districtname=="Bhabua"
replace districtname="Purnia" if districtname=="Purnea"
replace districtname="North Goa" if districtname=="Goa North"
replace districtname="South Goa" if districtname=="Goa South"
replace districtname="Bhiwani" if districtname=="Bhilwani"
replace districtname="Yamunanagar" if districtname=="Yamuna Nagar"
replace districtname="Sirmaur" if districtname=="Sirampur"
replace districtname="Baramula" if districtname=="Barmula"
replace districtname="Kathua" if districtname=="Kathus"
replace districtname="Kupwara" if districtname=="Kupwar"
replace districtname="Leh* (Ladakh)" if districtname=="Ladakh*"
replace districtname="Punch" if districtname=="Poonch"
replace districtname="Rajauri" if districtname=="Rajouri"
replace districtname="Dakshina Kannada" if districtname=="Dakshin Kannad"
replace districtname="Mandya" if districtname=="Mandhya"
replace districtname="Uttara Kannada" if districtname=="Uttar Kannad"
replace districtname="Alappuzha" if districtname=="Alappuzm"
replace districtname="Kasaragod" if districtname=="Kasargod"
replace districtname="Malappuram" if districtname=="Malapuram"
replace districtname="Thrissur" if districtname=="Trichur"
replace districtname="Thiruvananthapuram" if districtname=="Triruvanantha-"
replace districtname="E. Nimar (Khandwa)" if districtname=="Khandwa (E. Nimar)"
replace districtname="W. Nimar (Khargoan)" if districtname=="Khargone (W. Nimar)"
replace districtname="Shahdol" if districtname=="Shadol"
replace districtname="Nashik" if districtname=="Nasik"
replace districtname="Mokokchung" if districtname=="Mukokchung"
replace districtname="Zunheboto" if districtname=="Zunhehoto"
replace districtname="Anugul" if districtname=="Angul"
replace districtname="Balangir" if districtname=="Bolangir"
replace districtname="Baudh" if districtname=="Boudh"
replace districtname="Debagarh" if districtname=="Deogarh"
replace districtname="Gajapati" if districtname=="Gajipati"
replace districtname="Jagatsinghapur" if districtname=="Jagatsinghpura"
replace districtname="Jajapur" if districtname=="Jajpur"
replace districtname="Kendujhar" if districtname=="Keonjhar"
replace districtname="Khordha" if districtname=="Khurda"
replace districtname="Nabarangapur" if districtname=="Nowarangpur"
replace districtname="Nuapada" if districtname=="Nuapara"
replace districtname="Kandamal (Phoolbani)" if districtname=="Phulbani"
replace districtname="Ryagada" if districtname=="Rayagarh"
replace districtname="Sonapur" if districtname=="Sonepur"
replace districtname="Bathinda" if districtname=="Bhatinda"
replace districtname="Faridkot" if districtname=="Faridkot Sahib"
replace districtname="Dhaulpur" if districtname=="Dholpur"
replace districtname="Jhunjhunun" if districtname=="Jhunjhuna"
replace districtname="Chennai" if districtname=="Chengai Anna (Chengalpattu)"
replace districtname="Dharmapuri" if districtname=="Dharampuri"
replace districtname="Dindigul" if districtname=="Dindigul-Quide (Milleth Anna)"
replace districtname="Kanniyakumari" if districtname=="Kannyai-Kumari"
replace districtname="Nagapattinam" if districtname=="Nagapatti-nam-Quaie-E-Milleth"
replace districtname="Ramanathapuram" if districtname=="Ramnathapuram"
replace districtname="The Nilgiris" if districtname=="Nilgiri"
replace districtname="Tiruchirappalli" if districtname=="Tiruchirapalli"
replace districtname="Tirunelveli" if districtname=="Tirunelveli (Kottabomman)"
replace districtname="Tiruvanamalai" if districtname=="Thiruvannamalai"
replace districtname="Viluppuram" if districtname=="Villupuram"
replace districtname="Barabanki" if districtname=="Bara Banki"
replace districtname="Deoria" if districtname=="Deeria"
replace districtname="Fatehpur" if districtname=="Fatepur"
replace districtname="Jalaun" if districtname=="Jalan"
replace districtname="Rae Bareli" if districtname=="Rai Bareli"
replace districtname="Siddharthnagar" if districtname=="Sidhartha nagar"
replace districtname="Sonbhadra" if districtname=="Sonbadra"
replace districtname="Varanasi" if districtname=="Varanashi"
replace districtname="North 24-Parganas" if districtname=="24-Parganas (North)"
replace districtname="South 24-Parganas" if districtname=="24-Parganas (South)"
replace districtname="Hugli" if districtname=="Hooghly"
replace districtname="Koch Bihar" if districtname=="Kochbihar"
replace districtname="Medinipur" if districtname=="Midnapur"
replace districtname="Nadia" if districtname=="Nandia"
*replace districtname="Kolkata" if districtname=="Calcutta"
replace districtname="Chitrakoot" if districtname=="Chitrakut"
replace districtname="Hazaribag" if districtname=="Hazaribagh"
replace districtname="Pakaur" if districtname=="Pakur"
replace districtname="Rajnandgaon" if districtname=="Raj Nandgaon"
replace districtname="Ranchi" if districtname=="Ranchi (Purbi)"
replace districtname="Rayagada" if districtname=="Ryagada"
replace districtname="Sahibganj" if districtname=="Sahibaganj"
replace districtname="Singhbhum (E)" if districtname=="Singhbhum"
replace districtname="Singhbhum(W)" if districtname=="Singhbhum (Paschim)"
replace districtname="Sitamarhi" if districtname=="Sitamari"
replace districtname="Uttarkashi" if districtname=="Uttar Kashi"
replace districtname="Dehradun" if districtname=="Dehra Dun"
replace districtname="Kandhamal (Phoolbani)" if districtname=="Kandamal (Phoolbani)"

replace districtname="Ahmadnagar" if districtname=="Ahmednagar"
replace districtname="Alappuzha" if districtname=="Alapuzha"
replace districtname="Badgam" if districtname=="Bagdham"
replace districtname="Baleshwar" if districtname=="Balleshwar"
replace districtname="Barddhaman" if districtname=="Bardhaman"
replace districtname="Barpeta" if districtname=="Barpetta"
replace districtname="Bhandara" if districtname=="Bandhara"
replace districtname="Bhiwani" if districtname=="Bhiwania"
replace districtname="Bijnor" if districtname=="Bijore"
replace districtname="Bulandshahr" if districtname=="Bulandshahar"
replace districtname="Chittoor" if districtname=="Chitoor"
replace districtname="Churachandpur" if districtname=="Churachanpur"
replace districtname="Dakshina Kannada" if districtname=="Dakshinkannad"
replace districtname="Darjiling" if districtname=="Darjeeling"
replace districtname="Dindigul" if districtname=="Dindigulanna"
replace districtname="Doda" if districtname=="Dodha"
replace districtname="East (Gangtok)" if districtname=="East District"
replace districtname="East Godavari" if districtname=="East Godavri"
replace districtname="E. Nimar (Khandwa)" if districtname=="East Nimar"


replace districtname="East Garo Hills" if districtname=="Eastgarohills"
replace districtname="Farrukhabad" if districtname=="Farukabad"
replace districtname="Firozpur" if districtname=="Ferozepur"
replace districtname="Gadchiroli" if districtname=="Gadhchiroli"
replace districtname="Garhwal" if districtname=="Garhwhal"
replace districtname="Giridin" if districtname=="Girdin"
replace districtname="Godda" if districtname=="Godha"
replace districtname="Hisar" if districtname=="Hissar"
replace districtname="Jalgaon" if districtname=="Jalgaun"
replace districtname="Jalor" if districtname=="Jalore"
replace districtname="Jhabua" if districtname=="Jhabhua"
replace districtname="Kachchh" if districtname=="Kacchh"
replace districtname="Kanniyakumari" if districtname=="Kanyakumari"
replace districtname="Kapur Nagar" if districtname=="Kanpurnagar"
replace districtname="Karbi Anglong" if districtname=="Karbianglong"
replace districtname="Katihar" if districtname=="Katia"
replace districtname="Kinnaur" if districtname=="Kinmao"
replace districtname="Koch Bihar" if districtname=="Coch Bihar"
replace districtname="Kolhapur" if districtname=="Kolapur"
replace districtname="Kozhikode" if districtname=="Kozhigode"
replace districtname="Kullu" if districtname=="Kulu"
replace districtname="Lahul & Spiti" if districtname=="Lahaulspiti"
replace districtname="Lakshadweep" if districtname=="Lakshwadweep"
replace districtname="Lower Subansiri" if districtname=="Lower Subhanshari"
replace districtname="Maharajganj" if districtname=="Maharjganj"
replace districtname="Mahendragarh" if districtname=="Mahendranagar"
replace districtname="Maldah" if districtname=="Malda"
replace districtname="Mandya" if districtname=="Mandia"
replace districtname="Mandsaur" if districtname=="Mansaur"
replace districtname="Mon" if districtname=="Man"
replace districtname="Mokokchung" if districtname=="Mukochug"
replace districtname="Muzaffarpur" if districtname=="Muzafarpur"
replace districtname="Nicobars" if districtname=="Nicobar"
replace districtname="North Arcot" if districtname=="Northarcot"
replace districtname="North Cachar Hills" if districtname=="Northcacharhills"
replace districtname="North 24-Parganas" if districtname=="Northtwentyfourparaganas"
replace districtname="Palakkad" if districtname=="Palakad"
replace districtname="Panch Mahals" if districtname=="Panchmahal"

replace districtname="Champaran(W)" if districtname=="Paschim Champaran"
replace districtname="Pathanamthitta" if districtname=="Pathamamthtta"
replace districtname="Prakasam" if districtname=="Prakasham"
replace districtname="Pudukkottai" if districtname=="Pudukotai"
replace districtname="Punch" if districtname=="Punj"
replace districtname="Champaran(E)" if districtname=="Purbi Champaran"
replace districtname="Puruliya" if districtname=="Purulia"
replace districtname="Rae Bareli" if districtname=="Raibareili"
replace districtname="Ramanathapuram" if districtname=="Ramanathpuram"
replace districtname="Runagar" if districtname=="Roopnagar"
replace districtname="Sabar Kantha" if districtname=="Sabarkantha"
replace districtname="Saharanpupr" if districtname=="Sahranppur"
replace districtname="Saharsa" if districtname=="Sarsa"
replace districtname="Sawai Madhopur" if districtname=="Sawaimadhepur"
replace districtname="Siddharthnagar" if districtname=="Sidharthngar"
replace districtname="Sirmaur" if districtname=="Sirmaun"
replace districtname="Sonipat" if districtname=="Sonepat"
replace districtname="South 24-Parganas" if districtname=="Southtwentyfourparagans"
replace districtname="Tirunelveli" if districtname=="Thirunelveli"
replace districtname="Tiruvanamalai" if districtname=="Thiruvanamala"
replace districtname="Tuensang" if districtname=="Twesang"
replace districtname="Upper Subansiri" if districtname=="Upper Subhanshari"
replace districtname="Uttara Kannada" if districtname=="Uttarkannad"
replace districtname="Visakhapatnam" if districtname=="Vishakapatnam"
replace districtname="Vizianagaram" if districtname=="Vizianagram"
replace districtname="W. Nimar (Khargoan)" if districtname=="Kargaon"
replace districtname="Warangal" if districtname=="Warrangal"
replace districtname="West (Gyalshing)" if districtname=="West District"
replace districtname="West Garo Hills" if districtname=="Westgarohills"
replace districtname="West Godavari" if districtname=="West Godavri"
replace districtname="West Khasi Hills" if districtname=="Westkhasihills"
replace districtname="Wokha" if districtname=="Whoka"
replace districtname="Wayanad" if districtname=="Wyanad"
replace districtname="Zunheboto" if districtname=="Zoneboto"

replace districtname="Darrang" if districtname=="Dharang"
replace districtname="Hailakandi" if districtname=="Kaillakandi"
replace districtname="Nagaon" if districtname=="Naogaon"
replace districtname="Dadra & Nagar Haveli" if districtname=="Dadra"
replace districtname="Bans Kantha" if districtname=="Banaskantha"
replace districtname="Mahesana" if districtname=="Mehsana"
replace districtname="Shimla" if districtname=="Simla"
replace districtname="Baramula" if districtname=="Barmulla"
replace districtname="Singhbhum (E)" if districtname=="Paschimi Singhbum"
replace districtname="Singhbhum(W)" if districtname=="Purbi Singhbhum"
replace districtname="Ernakulam" if districtname=="Ernakulum"
replace districtname="Kollam" if districtname=="Kolam"
replace districtname="Thiruvananthapuram" if districtname=="Thirunavantha"
replace districtname="Chhindwara" if districtname=="Chindwara"
replace districtname="Damoh" if districtname=="Damu"
replace districtname="Sindhudurg" if districtname=="Sidhudurg"
replace districtname="Thoubal" if districtname=="Toubal"
replace districtname="Dhenkanal" if districtname=="Dhenkenal"
replace districtname="Sundargarh" if districtname=="Sundergarh"
replace districtname="Rupnagar" if districtname=="Runagar"
replace districtname="Chittaurgarh" if districtname=="Chittorgarh"
replace districtname="Jhalawar" if districtname=="Jalawar"
replace districtname="North (Mongam)" if districtname=="North District"
replace districtname="South (Nimachai)" if districtname=="South District"
replace districtname="Ballia" if districtname=="Balia"
replace districtname="Kanpur Nagar" if districtname=="Kapur Nagar"
replace districtname="Saharanpur" if districtname=="Saharanpupr"
replace districtname="Siddharthnagar" if districtname=="Sidharthnagar"
replace districtname="Sonbhadra" if districtname=="Sonebhadra"
replace districtname="Deogarh" if districtname=="Deagarh"
replace districtname="Srikakulam" if districtname=="Srikakalum"


preserve
merge m:m statename districtname using nss61_nm61
keep if _merge==3
drop _merge
save "`rootdir'districts\common", replace
restore


merge m:m stcode dist using "`rootdir'districts\common", update replace
drop _merge

*Uttarkhand-Uttar Pradesh
replace stcode=12 if stcode==16
replace statename="Uttar Pradesh" if stcode==12

*Jharkand-Bihar
replace stcode=2 if stcode==15
replace statename="Bihar" if stcode==2

*Chhatisgarh-Madhya Pradesh
replace stcode=6 if stcode==14
replace statename="Madhya Pradesh" if stcode==6

replace districtname="Raigad" if districtname=="Raigad / Kolaba"
replace dist=41 if statename=="Madhya Pradesh" & districtname=="Rajgarh"
replace districtname="Surguja" if districtname=="Sarguja"

duplicates drop statename districtname, force



keep stcode statename dist districtname


save district_conversion, replace









********Construct district-level variables from NSS/employment data**********
***Employment: 
***employment by industry [retail and transport, "formal sector(cash)"] 
***population



****Employment data
foreach j in 43 55 61 66{
use "`rootdir'round`j'_sch10food", clear
sort sector region fsu
bysort fsu sector: egen minddist=min(districtcode)
bysort fsu sector: egen maxddist=max(districtcode)
replace districtcode=minddist if districtcode==.
replace districtcode=maxddist if districtcode==0 & maxddist~=.

ren region regioncode
gen statecode=floor(regioncode/10)
tab statecode

merge m:m statecode districtcode using nss`j'_nm61
keep if _merge==3
drop _merge

ren statename statename`j'
ren districtname districtname`j'
merge m:m statename`j' districtname`j' using district_conversion



bysort statenamedist districtnamedist: egen nsspop=total(mult)
bysort statename43 districtname43: egen nsspop43=total(mult)

bysort statenamedist districtnamedist: egen foodwrk=total(food*mult)
bysort statename43 districtname43: egen foodwrk43=total(food*mult)

gen foodshare=foodwrk/nsspop
gen foodshare43=foodwrk43/nsspop43

bysort statenamedist districtnamedist: egen foodshare_uw=mean(food)
bysort statename43 districtname43: egen foodshare43_uw=mean(food)

duplicates drop statename43 districtname43, force
keep round statenamedist districtnamedist statename43 districtname43 nsspop* foodshare* foodwrk*
save nss_distdata_food_`j', replace
}




****Consumer data****
**generate district price index
**generate pop
**generate electric index
**generate hh variety and residual variety
**generate district level variety
*Program pools rural/urban areas of district

clear
save allhh, replace emptyok


foreach round in 43 55 61 66{
use round`round'_edit, clear
merge 1:1 sector subround region subsample segment substratum fsu hhno using round`round'_hh, keepusing(mpce hhsize d* nic* mult servant)
drop _merge
merge m:m region round using mult`round', keepusing(regioncode)
drop _merge

gen state=floor(region/10)

drop xfood
gen nfood=0
gen xfood=0
local list grains pulses meat milk oil veg fru sugar bev proc
foreach i in `list'{
replace nfood=nfood+n`i'
replace xfood=xfood+x`i'
}






drop if xfood==0 | xfood==.
drop if nfood==0 | nfood==.


gen nall=nfood+ncloth+nintox+nlight+ndur+nmisc
gen xall=xfood+xcloth+xintox+xlight+xdur+xmisc

gen electric=0
replace electric=1 if xlight_3>0
replace electric=1 if qlight_3>0 & qlight_3~=.

drop *cloth* *light* *intox* 

cap: gen district=.

gen districtcode=.
replace districtcode=district
cap: replace districtcode=stratum if round==50

gen statecode=state


sort sector region fsu
bysort fsu sector: egen minddist=min(districtcode)
bysort fsu sector: egen maxddist=max(districtcode)
replace districtcode=minddist if districtcode==.
replace districtcode=maxddist if districtcode==0 & maxddist~=.


merge m:m statecode districtcode using nss`round'_nm61
keep if _merge==3
drop _merge

ren statename statename`round'
ren districtname districtname`round'

merge m:m statename`round' districtname`round' using district_conversion
keep if _merge==3
drop _merge

merge m:1 statenamedist districtnamedist round using pindex_districts_revised
keep if _merge==3
drop _merge



****generate "presence" measures (and district sample hh count and district sample mean exp.)
gen vnumfood=0
gen dnumfood=0
gen dsnumfood=0
gen rnumfood=0
gen rsnumfood=0

gen vxfoodpc=0
gen dxfoodpc=0
gen dsxfoodpc=0
gen rxfoodpc=0
gen rsxfoodpc=0

gen dxfood=0
gen dsxfood=0
gen rxfood=0
gen rsxfood=0



foreach i in grains pulses meat milk oil veg fru sugar bev proc{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
bysort statenamedist districtnamedist: egen meduv`i'_`j'=median(x`i'_`j'/q`i'_`j')
}
}

*dispersion in exp. shares
foreach i in grains pulses meat milk oil veg fru sugar bev proc{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
gen share`i'_`j'=x`i'_`j'/xfood
replace share`i'_`j'=. if share`i'_`j'==1 | share`i'_`j'==0
}
}

aorder

*dispersion in unit values
foreach j in share meduv{
egen `j'_sd=rowsd(`j'bev_1-`j'veg_29)
egen `j'_mean=rowmean(`j'bev_1-`j'veg_29)
gen `j'_cv=`j'_sd/`j'_mean
}

*average or median of the "slope"?
foreach j in sd mean cv{
bysort statenamedist districtnamedist: egen medshare_`j'=median(share_`j')
bysort statenamedist districtnamedist: egen meanshare_`j'=mean(share_`j')
}



*Group level dispersions
*dispersion in exp. shares
foreach i in grains veg{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
cap: drop share`i'_`j'
gen share`i'_`j'=x`i'_`j'/x`i'
replace share`i'_`j'=. if share`i'_`j'==1 | share`i'_`j'==0
}
}
aorder
*dispersion in unit values
foreach j in share meduv{
egen `j'grains_sd=rowsd(`j'grains_1-`j'grains_18)
egen `j'grains_mean=rowmean(`j'grains_1-`j'grains_18)
gen `j'grains_cv=`j'grains_sd/`j'grains_mean

egen `j'veg_sd=rowsd(`j'veg_1-`j'veg_29)
egen `j'veg_mean=rowmean(`j'veg_1-`j'veg_29)
gen `j'veg_cv=`j'veg_sd/`j'veg_mean
}
*average or median of the "slope"
foreach j in cv{
bysort statenamedist districtnamedist: egen medsharegrains_`j'=median(sharegrains_`j')
bysort statenamedist districtnamedist: egen medshareveg_`j'=median(shareveg_`j')
}


foreach i in grains pulses meat milk oil veg fru sugar bev proc{
levelsof n`i'tot, local(nmax)
forvalues j=1(1)`nmax'{ 
bysort fsu: egen zmax=max(x`i'_`j')
replace vnumfood=vnumfood+1 if zmax>0
drop zmax
by fsu: egen zmax=total(x`i'_`j'*mult)
by fsu: egen zmax2=total(mult)
replace vxfoodpc=vxfoodpc+(zmax/zmax2)
drop zmax zmax2

bysort statenamedist districtnamedist: egen zmax=max(x`i'_`j')
replace dnumfood=dnumfood+1 if zmax>0 
drop zmax
by statenamedist districtnamedist: egen zmax=total(x`i'_`j'*mult)
replace dxfood=dxfood+zmax if zmax>0
by statenamedist districtnamedist: egen zmax2=total(mult)
replace dxfoodpc=dxfoodpc+(zmax/zmax2)
drop zmax zmax2

bysort statenamedist districtnamedist sector: egen zmax=max(x`i'_`j')
replace dsnumfood=dsnumfood+1 if zmax>0
* & zmax~=.
drop zmax
by statenamedist districtnamedist sector: egen zmax=total(x`i'_`j'*mult)
replace dsxfood=dsxfood+zmax if zmax>0
by statenamedist districtnamedist sector: egen zmax2=total(mult)
replace dsxfoodpc=dsxfoodpc+(zmax/zmax2)
drop zmax zmax2

bysort regioncode: egen zmax=max(x`i'_`j')
replace rnumfood=rnumfood+1 if zmax>0
* & zmax~=.
drop zmax
by regioncode: egen zmax=total(x`i'_`j'*mult)
replace rxfood=rxfood+zmax if zmax>0 
by regioncode: egen zmax2=total(mult)
replace rxfoodpc=rxfoodpc+(zmax/zmax2)
drop zmax zmax2

bysort regioncode sector: egen zmax=max(x`i'_`j')
replace rsnumfood=rsnumfood+1 if zmax>0 
*& zmax~=.
drop zmax
by regioncode sector: egen zmax=total(x`i'_`j'*mult)
replace rsxfood=rsxfood+zmax if zmax>0 
by regioncode sector: egen zmax2=total(mult)
replace rsxfoodpc=rsxfoodpc+(zmax/zmax2)
drop zmax zmax2
}
}

bysort fsu: gen vnumhh=_N
bysort statenamedist districtnamedist: gen dnumhh=_N
bysort statenamedist districtnamedist sector: gen dsnumhh=_N
bysort regioncode: gen rnumhh=_N
bysort regioncode sector: gen rsnumhh=_N



foreach i in grains veg{
gen vnum`i'=0
gen dnum`i'=0
gen rnum`i'=0
levelsof n`i'tot, local(nmax)
forvalues j=1(1)`nmax'{ 
bysort fsu: egen zmax=max(x`i'_`j')
replace vnum`i'=vnum`i'+1 if zmax>0
drop zmax 
bysort  statenamedist districtnamedist: egen zmax=max(x`i'_`j')
replace dnum`i'=dnum`i'+1 if zmax>0
drop zmax
bysort  regioncode: egen zmax=max(x`i'_`j')
replace rnum`i'=rnum`i'+1 if zmax>0
drop zmax
}

bysort statenamedist districtnamedist: egen dvmeannum`i'=mean(vnum`i')
bysort statenamedist districtnamedist: egen dvmediannum`i'=median(vnum`i')

bysort regioncode: egen rvmeannum`i'=mean(vnum`i')
bysort regioncode: egen rvmediannum`i'=median(vnum`i')


}



keep mult electric *pindex* mpce totexp xall xfood nfood hhsize sector statenamedist districtnamedist regioncode round *numfood *food *foodpc fsu medshare_* meduv_* meanshare_* *numhh *servant* dnum* d*num* *vnum* meduv* medshare*



append using allhh
save allhh, replace
}





use allhh, clear
local agg statenamedist districtnamedist round

bysort `agg': gen numshh=_N
bysort `agg': egen numhh=total(mult)


*share of households with electricity
bysort `agg': egen share_elec=total(mult*electric)
replace share_elec=share_elec/numhh

replace mpce=mpce/qpindex
replace totexp=xall/qpindex
replace xfood=xfood/qpindexfood


bysort `agg': egen meanpce=total(mpce*mult)
bysort `agg': egen totmult=total(mult)
replace meanpce=meanpce/totmult

bysort `agg': egen meanxfood=total(xfood*mult)
replace meanxfood=meanxfood/totmult

bysort `agg': egen meanhhsize=total(hhsize*mult)
replace meanhhsize=meanhhsize/totmult

bysort `agg': egen meannfood=total(nfood*mult)
replace meannfood=meannfood/totmult



bysort `agg': egen nsspop=total(mult*hhsize)
bysort `agg': egen nssexp=total(mult*totexp)


gen urban=0
replace urban=1 if sector==2
bysort `agg': egen nssurbanshare=total(mult*hhsize*urban)
replace nssurbanshare=nssurbanshare/nsspop


gen lognfood=log(nfood)
gen logxfood=log(xfood)
gen logexp=log(totexp)
gen logmpce=log(mpce)
gen loghhsize=log(hhsize)
gen loghhsize2=loghhsize^2


egen aggcounter=group(`agg')

areg lognfood logxfood loghhsize loghhsize2, absorb(aggcounter)
predict residn_xfood, d

areg lognfood logexp loghhsize loghhsize2, absorb(aggcounter)
predict residn_totx, d

areg lognfood logmpce loghhsize loghhsize2, absorb(aggcounter)
predict residn_mpce, d

bysort aggcounter: egen mean_nfood=mean(nfood)
bysort aggcounter: egen mean_lognfood=mean(lognfood)


preserve

keep `agg'  residn_* mean* pindex* round nssexp nsspop share_elec nssurbanshare numhh numshh regioncode round *numfood *food *foodpc fsu mult electric mpce totexp xall xfood nfood hhsize sector statenamedist districtnamedist round medshare_* meduv_* meanshare_* *numhh *servant*  d*num* dnum* *vnum* meduv* medshare*

gen year=.
replace year=1987 if round==43
replace year=1993 if round==50
replace year=1999 if round==55
replace year=2004 if round==61
replace year=2009 if round==66

save var_districts_allhh, replace






use nss66_nm61, clear
merge m:m statename districtname using "`rootdir'districts\district_poparea2011"
keep if _merge==3
drop _merge
ren statename statename66
ren districtname districtname66
merge m:m statename66 districtname66 using district_conversion
drop if statenamedist==""
drop if districtnamedist==""
bysort statenamedist districtnamedist: egen totarea=total(areasqkm)
duplicates drop statenamedist districtnamedist, force
keep statenamedist districtnamedist totarea
summ
save "`rootdir'districts\olddist_area", replace


***Fix errors and interpolate missing census variables*****

use districtdata_clean, clear


drop if year<1981


sort stname distname year

***1. cubic interpolation
gen lroad3=.
by stname distname: gen period=_n
gen period2=period^2
gen period3=period^3
egen dist2=group(stname distname)
egen distmax=max(dist2)
local maxdist=distmax[1]
forvalues j=1(1)`maxdist'{
cap: reg lroad period period2 period3 if dist2==`j' & year<2010
cap: predict temp, xb
cap: replace lroad3=temp if dist2==`j'
cap: drop temp
}
drop period period2 period3 dist2

gen diff=abs((lroad3-lroad)/lroad)
replace lroad3=. if diff>0.3


gen flag=0

forvalues j=1(1)20{
gen dlroad`j'=(lroad-lroad[_n-`j'])/lroad[_n-`j'] if stname==stname[_n-`j'] & distname==distname[_n-`j'] & year==year[_n-`j']+`j'
replace flag=1 if dlroad`j'<-0.1
}
gen lroad2=lroad
replace lroad2=. if flag==1

foreach j of varlist ppmrkt-lroad2{
by stname distname: ipolate `j' year, gen(`j'_i)
replace `j'=`j'_i
drop `j'_i
}


merge m:m stcode dist using district_conversion
drop _merge distname stname
ren statename statenamedist
ren districtname districtnamedist
save temp, replace





use nss_distdata_food_43, clear
gen year=1987
append using nss_distdata_food_55
replace year=1999 if year==.
append using nss_distdata_food_61
replace year=2004 if year==.
append using nss_distdata_food_66
replace year=2009 if year==.
keep statenamedist districtnamedist year foodshare foodshare_uw foodwrk nsspop
duplicates drop statenamedist districtnamedist year, force
save nss_distdata_food, replace









*****Analysis of district-level variety and co-variates:  Table 2*****

local agg round statenamedist districtnamedist

use var_districts_allhh, clear

*bring in employment
merge m:1 statenamedist districtnamedist year using nss_distdata_food
drop _merge
*bring in area (for cross-section)
merge m:m statenamedist districtnamedist using "`rootdir'districts\olddist_area"
drop _merge
*bring in roads/markets/census data
merge m:m statenamedist districtnamedist year using temp

*dropping all the other years here
keep if _merge==3
drop _merge


*drop 1993: not good for over time comparisons due to lack of urban areas
drop if year==1993
*drop 55th round due to reporting period/comparability issues over time
drop if round==55


drop if statenamedist==""
drop if districtnamedist==""
gen fd=substr(districtnamedist,1,1)
drop if fd=="?"

drop dist
egen dist=group(statenamedist districtnamedist)



gen lnmeannfood=log(meannfood)
gen lnnfood=ln(nfood)
gen lnhhsize=ln(hhsize)
gen lndnumhh=ln(dnumhh)
gen lnvnumhh=ln(vnumhh)
gen lndnumfood=ln(dnumfood)
gen lnvnumfood=ln(vnumfood)

***deflate expenditures by price index

gen lnxfood=log(xfood/pindexfood)
gen lnmeanxfood=log(meanxfood/pindexfood)

gen lnmpce=log(mpce/pindex)
gen lnmeanpce=log(meanpce/pindex)

gen lnvxfoodpc=ln(vxfoodpc/pindexfood)
gen lndxfoodpc=ln(dxfoodpc/pindexfood)


gen lnmeduv_cv=ln(meduv_cv)
gen lnmeanshare_cv=ln(meanshare_cv)
gen lnmedshare_cv=ln(medshare_cv)




gen urbanshare=pop_ub/pop_t
gen pop=pop_t

*turn things into densities?
foreach j in lroad lroad2 lroad3 ppmrkt psmrkt ptmrkt nsspop pop{
gen `j'sq=ln(`j'/totarea)
}

gen foodwrksq=ln(foodwrk/totarea)



gen urban=0
replace urban=1 if sector==2


gen servantx=0
replace servantx=1 if xservant>0
tab servantx if servant==1
tab servant if servantx==1
corr servant servantx
gen anyservant=0
replace anyservant=1 if servantx==1 | servant==1

bysort year sector: summ anyservant

cap: gen insample=1
foreach j in lnnfood lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lndnumfood lnvnumfood lnmedshare_cv {
replace insample=0 if `j'==.
}






****Table 2****

local restrict i.year if insample==1 
local service

reg lnnfood lnxfood lnhhsize `service'  `restrict', vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize `service') replace ctitle(Dist)

reg lnnfood lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv   `service'  `restrict', vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq `service' lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv) append ctitle(Dist)

reg lnnfood lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv  lndnumfood lnvnumfood `service'  `restrict', vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq `service' lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv lndnumfood lnvnumfood) append ctitle(Dist)

reghdfe lnnfood lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv `service' `restrict', absorb(year dist) vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv) append ctitle(Dist FE)

reghdfe lnnfood lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv lndnumfood lnvnumfood `service' `restrict', absorb(year dist) vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv lndnumfood lnvnumfood) append ctitle(Dist FE)





**Appendix: Table A.1 *
local restrict i.year if insample==1 & anyservant==0 
local service anyservant
reg lnnfood lnxfood lnhhsize `service'  `restrict', vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize `service') replace ctitle(Dist)
reg lnnfood lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv  lndnumfood lnvnumfood  `service'  `restrict', vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv  lndnumfood lnvnumfood) append ctitle(Dist)
reghdfe lnnfood lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv `service' `restrict', absorb(year dist) vce(cluster dist)
outreg2 using table2rev`service'.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv `service') append ctitle(Dist FE)

local restrict i.year if insample==1 
local service
reg anyservant lnxfood lnhhsize  `restrict', vce(cluster dist)
outreg2 using table2revanyservant.tex, dec(3) keep(lnxfood lnhhsize) append ctitle(servant)
reg anyservant lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv lndnumfood lnvnumfood `restrict', vce(cluster dist)
outreg2 using table2revanyservant.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv lndnumfood lnvnumfood) append ctitle(servant)
reghdfe anyservant lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv `restrict', absorb(year dist)  vce(cluster dist)
outreg2 using table2revanyservant.tex, dec(3) keep(lnxfood lnhhsize lnmeanpce nsspopsq lroad2sq foodshare_uw lnmeduv_cv lnmedshare_cv ) append ctitle(servant FE)


tab anyservant if insample==1
*4.6% of sample employ servants


























*****Constructing rainfall by region, aggregated up from districts to regions*****
**need the mapping from districts (consistent districts, weighted by land) to (consistent) regions/states


*****Construct mapping

use mult43, clear
drop if regioncode==.
replace regioncode=71 if region==54

duplicates drop region regioncode, force

keep region regioncode
save temp43a, replace


use nss43_nm61, clear
drop if statecode==.
duplicates drop statename districtname, force
save temp43b, replace


use district_conversion, clear
merge 1:1 statename districtname using temp43b
keep if _merge==3
drop _merge
ren regioncode region
merge m:1 region using temp43a
keep if _merge==3
drop _merge
*regioncode is the unique and consistent  region*state identifier

 keep stcode dist regioncode
 drop if dist==.
 duplicates drop stcode dist, force
 save dist_region, replace
 


***Final output: file with land and population (from district database) matched to the common NSS region codes in mult.
**[note: using the census weights may also mean we don't have to worry about dropping anyone at this stage, though district
**database does not cover everywhere so there will be drops eventually]


****Construct NSS region level measures from district data
use "`rootdir'districts\alldata_nm61_inter", clear
merge m:1 stcode dist using dist_region

**now create region rainfall measures
**normal rainfall --
**this year's rainfall --

gen julaugrf_norm=july+aug
bysort stcode dist: egen julaug_mean=mean(julaugrf)

summ julaugrf julaugrf_norm julaug_mean, detail

sort stcode dist year
drop if year<1983
gen insample=0
replace insample=1 if julaugrf~=. & julaug_mean~=.

*land weights:
replace gca=. if insample==0
replace gca=. if gca==0
bysort regioncode year: egen totland=total(gca)
gen landshare=gca/totland

replace gia=. if insample==0

gen noniland=gca-gia
replace noniland=. if noniland<=0
bysort regioncode year: egen totniland=total(noniland)
gen nilandshare=noniland/totniland

replace pop_t=. if insample==0
bysort regioncode year: egen totpop=total(pop_t)
gen popshare=pop_t/totpop

summ landshare nilandshare popshare, detail

*generate index
bysort regioncode year: egen norm_rain_gca=total(landshare*julaug_mean)
bysort regioncode year: egen norm_rain_gia=total(nilandshare*julaug_mean)
bysort regioncode year: egen norm_rain_pop=total(popshare*julaug_mean)
bysort regioncode year: egen act_rain_gca=total(landshare*julaugrf)
bysort regioncode year: egen act_rain_gia=total(nilandshare*julaugrf)
bysort regioncode year: egen act_rain_pop=total(popshare*julaugrf)

bysort regioncode: egen reglat=mean(lat)
bysort regioncode: egen reglon=mean(lon)


keep regioncode year norm_rain_* act_rain_* reglat reglon
*rain can't be zero?
foreach j of varlist norm_rain_gca-act_rain_pop{
replace `j'=. if `j'==0
}

duplicates drop regioncode year, force
keep if year==1983 | year==1987 | year==1993 | year==1999 | year==2004 | year==2009 
summ norm* act* reglat reglon, detail
save region_rain, replace





****For 1999-2000 generate district variables to match to TUS for shopping time analysis
****include price indexes, availability, average variety, residual variety, and EC slope


cd "`rootdir'"


local round 55
use round`round'_edit, clear
merge 1:1 sector subround region subsample segment substratum fsu hhno using round`round'_hh, keepusing(mpce hhsize d* nic* mult servant)
drop _merge
merge m:m region round using mult`round', keepusing(regioncode)
drop _merge

gen state=floor(region/10)

drop xfood
gen nfood=0
gen xfood=0
local list grains pulses meat milk oil veg fru sugar bev proc
foreach i in `list'{
replace nfood=nfood+n`i'
replace xfood=xfood+x`i'
}


drop if xfood==0 | xfood==.
drop if nfood==0 | nfood==.


gen nall=nfood+ncloth+nintox+nlight+ndur+nmisc
gen xall=xfood+xcloth+xintox+xlight+xdur+xmisc


drop *cloth* *light* *intox* 

cap: gen district=.
gen districtcode=.
replace districtcode=district
cap: replace districtcode=stratum if round==50
gen statecode=state
sort sector region fsu
bysort fsu sector: egen minddist=min(districtcode)
bysort fsu sector: egen maxddist=max(districtcode)
replace districtcode=minddist if districtcode==.
replace districtcode=maxddist if districtcode==0 & maxddist~=.


merge m:m statecode districtcode using nss`round'_nm61
keep if _merge==3
drop _merge

ren statename statename`round'
ren districtname districtname`round'

merge m:m statename`round' districtname`round' using district_conversion
keep if _merge==3
drop _merge

merge m:1 statenamedist districtnamedist round using pindex_districts_revised
keep if _merge==3
drop _merge


ren statename66 statename
ren districtname66 districtname
merge m:1 statename districtname using "`rootdir'districts\district_poparea2011", keepusing(pop areasqkm densitysqkm)
drop _merge
ren pop popcensus


gen year=1999



merge m:1 statenamedist districtnamedist year using nss_distdata_sch10
drop _merge
*bring in area (for cross-section)
merge m:m statenamedist districtnamedist using "`rootdir'districts\olddist_area"
drop _merge
*bring in roads/markets/census data
merge m:m statenamedist districtnamedist year using temp

drop if year~=1999
drop _merge



gen urbanshare=pop_ub/pop_t
gen pop=pop_t

*turn things into densities?
foreach j in lroad lroad2 lroad3 ppmrkt psmrkt ptmrkt pop{
gen `j'sq=ln(`j'/totarea)
}

gen num67wrk=numpr_6+numsub_6+numpr_7+numsub_7
*share of the workforce (some zeros, especially for subsidiary employment, so can't use logs!)
foreach j in numpr_6 numsub_6 numpr_7 numsub_7 numpr_1 numsub_1 num67wrk{
gen `j'sq=ln(`j'/totarea)
}


renpfix pindex altpindex


**Keep just the states in the time-use data
gen tstate=1 if stname=="Haryana"
replace tstate=2 if stname=="Madhya Pradesh"
replace tstate=3 if stname=="Gujarat"
replace tstate=4 if stname=="Orissa"
replace tstate=5 if stname=="Tamil Nadu"
replace tstate=6 if stname=="Meghalaya"
drop if tstate==.

drop state stname
ren tstate state

**generate district price index
**generate agg availability
local list grains pulses milk meat oil veg fru sugar bev proc

drop xfood
gen nfood=0
gen xfood=0
foreach i in `list'{
replace nfood=nfood+n`i'
replace xfood=xfood+x`i'
}
drop if xfood==0 | xfood==.
drop if nfood==0 | nfood==.



local agg state district sector

***Construct aggregate availability measure by district


bysort `agg': gen numshh=_N
bysort `agg': egen numhh=total(mult)

foreach i in `list'{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
gen p`i'_`j'=0
replace p`i'_`j'=1 if x`i'_`j'>0 & x`i'_`j'<.
}
}


gen aggnfood=0

foreach i in `list'{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
bysort `agg': egen present`i'_`j'=max(p`i'_`j')
bysort `agg': egen ext`i'_`j'=total(p`i'_`j'*mult)
bysort `agg': egen aggx`i'_`j'=total(x`i'_`j'*mult)
egen baggx`i'_`j'=total(x`i'_`j'*mult)
replace aggnfood=aggnfood+present`i'_`j'
drop p`i'_`j'
}
}

**Construct price relative to aggregate**
**what is the base price (or exp. share?)  -- for now just use the aggregate of all sample districts
bysort `agg': egen aggxfood=total(xfood*mult)
egen baggxfood=total(xfood*mult)

foreach i in `list'{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
bysort `agg': egen meduv`i'_`j'=median(x`i'_`j'/q`i'_`j')
egen bmeduv`i'_`j'=median(x`i'_`j'/q`i'_`j')

gen share`i'_`j'=aggx`i'_`j'/aggxfood
gen bshare`i'_`j'=baggx`i'_`j'/baggxfood
drop x`i'_`j' q`i'_`j'

replace share`i'_`j'=0 if share`i'_`j'==.
replace bshare`i'_`j'=0 if bshare`i'_`j'==.
}
}

gen pindex=0
foreach i in `list'{
local nmax=n`i'tot[1]
forvalues j=1(1)`nmax'{
gen relprice`i'_`j'=log(meduv`i'_`j'/bmeduv`i'_`j')
replace relprice`i'_`j'=0 if relprice`i'_`j'==. 
gen weight`i'_`j'=(share`i'_`j'+bshare`i'_`j')/2
replace pindex=pindex+(weight`i'_`j'*relprice`i'_`j')
}
}

replace pindex=exp(pindex)
summ pindex, detail


bysort `agg': egen meanpce=total(mpce*mult)
bysort `agg': egen totmult=total(mult)
replace meanpce=meanpce/totmult

bysort `agg': egen meanxfood=total(xfood*mult)
replace meanxfood=meanxfood/totmult

bysort `agg': egen meanhhsize=total(hhsize*mult)
replace meanhhsize=meanhhsize/totmult

bysort `agg': egen meann=total(nfood*mult)
replace meann=meann/totmult

bysort `agg': egen mediann=median(nfood)


**residual variety (intercept), slope

gen logn=log(nfood)
gen logxfood=log(xfood/pindex)
gen logexp=log(totexp/pindex)
gen logmpce=log(mpce/pindex)
gen loghhsize=log(hhsize)
gen loghhsize2=loghhsize^2


duplicates drop `agg', force

keep `agg' slope_* int_* residn_* mean* pindex* agg* ext* present* meduv* n* lroadsq lroad2sq lroad3sq num67wrksq popsq *mrktsq altpindex* popcensus areasqkm densitysqkm statename districtname
save var_districts_tus, replace


